﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EcoSystem.Permissions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.Application.Dtos;

namespace EcoSystem.ConfigSettings;

/// <summary>
/// 系统配置管理
/// </summary>
[Area(EcoSystemRemoteServiceConsts.ModuleName)]
[RemoteService(Name = EcoSystemRemoteServiceConsts.RemoteServiceName)]
[Route("api/Platform/configSetting")]
public class ConfigSettingController : EcoSystemController, IConfigSettingAppService
{
    private readonly IConfigSettingAppService _service;

    public ConfigSettingController(IConfigSettingAppService service)
    {
        _service = service;
    }

    /// <summary>
    /// 创建
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ConfigSettingDto> CreateAsync(CreateOrUpdateConfigSettingDto input)
    {
        return await _service.CreateAsync(input);
    }

    /// <summary>
    /// 获取详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("{id}")]
    public async Task<ConfigSettingDto> GetAsync(Guid id)
    {
        return await _service.GetAsync(id);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="id"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("{id}")]
    public async Task<ConfigSettingDto> UpdateAsync(Guid id, CreateOrUpdateConfigSettingDto input)
    {
        return await _service.UpdateAsync(id, input);
    }


    /// <summary>
    /// 列表查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<PagedResultDto<ConfigSettingDto>> GetListAsync(GetConfigSettingInput input)
    {
        return await _service.GetListAsync(input);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("{id}")]
    public async Task DeleteAsync(Guid id)
    {
        await _service.DeleteAsync(id);
    }

    /// <summary>
    /// 获取配置项项信息
    /// </summary>
    /// <param name="input">参数</param>
    /// <returns></returns>
    [HttpGet]
    [Route("getConfigSetting")]
    public async Task<ConfigDefineAndSettingDto> GetConfigSettingAsync(GetConfigDefineAndSettingInputDto input)
    {
        return await _service.GetConfigSettingAsync(input);
    }

    /// <summary>
    /// 获取用户配置信息
    /// </summary>
    /// <param name="input">参数</param>
    /// <returns></returns>
    [HttpGet]
    [Route("getUserConfigSetting")]
    public async Task<ConfigDefineAndSettingDto> GetUserConfigSettingAsync(GetConfigDefineAndSettingInputDto input)
    {
        return await _service.GetUserConfigSettingAsync(input);
    }

    /// <summary>
    /// 保存指定分类对应的全局配置、指定系统的系统级配置或用户级配置
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("saveConfig")]
    public async Task<bool> SaveConfigAsync(SaveSettingsInput input)
    {
        return await (_service.SaveConfigAsync(input));
    }

    /// <summary>
    /// 保存全部配置数据
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("saveAllConfig")]
    public async Task<bool> SaveAllConfigAsync(SaveAllSettingsInput input)
    {
        return await (_service.SaveAllConfigAsync(input));
    }

    /// <summary>
    ///保存指定分类对应的用户配置数据
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("saveUserConfig")]
    public async Task<bool> SaveUserConfigAsync(SaveUserSettingsInput input)
    {
        return await _service.SaveUserConfigAsync(input);
    }

    /// <summary>
    /// 获取配置数据
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("getConfig")]
    public async Task<Dictionary<string, Dictionary<string, object>>> GetConfigAsync(GetSettingsInput input)
    {
        return await _service.GetConfigAsync(input);
    }

    /// <summary>
    /// 获取指定的配置键对应的配置值
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("getValue")]
    public async Task<string> GetConfigValueAsync(GetConfigValueInputDto input)
    {
        return await _service.GetConfigValueAsync(input);
    }

    /// <summary>
    /// 保存指定的配置键值
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("setValue")]
    public async Task<bool> SetConfigValueAsync(SetConfigValueInputDto input)
    {
        return await _service.SetConfigValueAsync(input);
    }


    /// <summary>
    /// 按分类批量删除
    /// </summary>
    /// <param name="category"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("deleteAll")]
    public async Task DeleteAllAsync(string category)
    {
        await _service.DeleteAllAsync(category);
    }

    /// <summary>
    /// 删除指定键的全部数据
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("deleteForKey")]
    public async Task DeleteForKeyAsync(string key)
    {
        await _service.DeleteForKeyAsync(key);
    }
}
